﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace HYRElectric.Dimension
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdSpaceTagInsert : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result;
			using (DlgSpaceTagInsert dlgSpaceTagInsert = new DlgSpaceTagInsert(cmdData.Application))
			{
				try
				{
					while (dlgSpaceTagInsert.ShowDialog() == DialogResult.OK)
					{
						Family family = ((DbFamily)dlgSpaceTagInsert.cbxSymbol.SelectedItem).Load(this.m_doc);
						FamilySymbol fs = null;
						using (List<FamilySymbol>.Enumerator enumerator = family.GetSymbols().GetEnumerator())
						{
							if (enumerator.MoveNext())
							{
								fs = enumerator.Current;
							}
						}
						if (!dlgSpaceTagInsert.rbSelection.Checked)
						{
							this.SingleTagInsert(cmdData, dlgSpaceTagInsert.tbxTag.Text.Trim(), fs);
						}
						else
						{
							this.SelectionTagInsert(cmdData, dlgSpaceTagInsert.tbxTag.Text.Trim(), fs);
						}
					}
				}
				catch (Exception)
				{
				}
				result = base.YJKExcute(cmdData, ref message, elements);
			}
			return result;
		}

		private void SingleTagInsert(ExternalCommandData cmdData, string strTag, FamilySymbol fs)
		{
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			try
			{
				for (;;)
				{
                    Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new Assist.SpaceFilter(), "请选择需要标记的空间");
					Space space = this.m_uiDoc.Document.GetElementById(reference.ElementId) as Space;
					this.DoInsert(strTag, space, fs);
				}
			}
			catch
			{
			}
		}

		private void SelectionTagInsert(ExternalCommandData cmdData, string strTag, FamilySymbol fs)
		{
            foreach (Reference reference in cmdData.Application.ActiveUIDocument.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new Assist.SpaceFilter(), "请选择要标记的空间"))
			{
				Space space = this.m_uiDoc.Document.GetElementById(reference.ElementId) as Space;
				this.DoInsert(strTag, space, fs);
			}
		}

		private void DoInsert(string strTag, Space space, FamilySymbol tagSymbol)
		{
			try
			{
				if (space != null)
				{
					if (strTag == "<默认>")
					{
						this.ImpSpaceTagInsert(this.m_uiDoc, space, tagSymbol.Id);
					}
					else if (strTag != "")
					{
						this.ImpSpaceTagInsert(this.m_uiDoc, space, tagSymbol.Id);
					}
				}
			}
			catch
			{
			}
		}

		private void ImpSpaceTagInsert(UIDocument m_uiDoc, Space space, ElementId tId)
		{
			Transaction transaction = new Transaction(m_uiDoc.Document, "TAG");
			transaction.Start();
			try
			{
				foreach (Element element in ElementFiltering.FindSpecialElements(m_uiDoc.Document, typeof(SpatialElementTag), BuiltInCategory.OST_MEPSpaceTags))
				{
					if (((SpaceTag)element).Space.Name == space.Name && ((SpaceTag)element).Space.Number == space.Number)
					{
						m_uiDoc.Document.DeleteElement(element);
					}
				}
				m_uiDoc.Document.Create.NewSpaceTag(space, new UV(((LocationPoint)space.Location).Point.X, ((LocationPoint)space.Location).Point.Y), m_uiDoc.ActiveView).ChangeTypeId(tId);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}
	}
}
